<?php

class Users_ManagementController extends Lizard_Crud_Controller_Management
{
    /**
     * @see Lizard_Entities_Controller_Management::indexAction()
     */
    public function indexAction()
    {
        $columns = array('id', 'firstName', 'lastName', 'role', 'status', 'registered');
        $this->getTable()->columns($columns);

        $this->view->columns = $columns;

        if ($this->_request->isXmlHttpRequest()) {

            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender();

            $data = $this->_helper->dataTables($this->getTable());
            $url = $this->_helper->url;

            $userId = Zend_Auth::getInstance()->getIdentity()->id;
            foreach ($data['aaData'] as &$rowData) {
                $params = array('id' => $rowData['id']);

                $edit = $url->url($params, 'users-management-edit');
                $rowData['editUrl'] = '<a href="'.$edit.'">Edit</a>';
                $rowData['deleteUrl'] = '';

                if ($rowData['id'] != $userId) {
                    $delete = $url->url($params, 'users-management-delete');
                    $rowData['deleteUrl'] = '<a href="'.$delete.'">Delete</a>';
                }
            }
            echo $this->_helper->json($data);
        }
    }

    /**
     * Delete category item
     *
     */
    public function deleteAction()
    {
        $userId = Zend_Auth::getInstance()->getIdentity()->id;
        if ($this->getIdentifier() == $userId) {
            $this->_notFound();
        }
        return parent::deleteAction();
    }
}